package com.huihuiyun.huihuiyun.server;

import com.huihuiyun.huihuiyun.server.domain.*;
import com.huihuiyun.huihuiyun.server.service.*;
import com.mark.util.CommonUtil;
import com.mark.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Vector;

@SpringBootApplication
@Slf4j
public class Application {
    @Autowired
    private HhyBookService bookService;

    @Autowired
    private HhyMemberService hhyMemberService;

    @Autowired
    private HhyAreaService hhyAreaService;

    @Autowired
    private HhyAdvertisementService hhyAdvertisementService;

    @Autowired
    private HhyProductCategoryService hhyProductCategoryService;

    @Autowired
    private HhyProductService hhyProductService;

    @Autowired
    private HhySellerService hhySellerService;

    @Autowired
    private HhyTemplateService hhyTemplateService;

    @Autowired
    private HhyWalletService hhyWalletService;

    @Autowired
    private HhyAgentService hhyAgentService;

    @Autowired
    private HhyOrderService hhyOrderService;

    @Autowired
    private HhyManagerService hhyManagerService;

    @Value("${spring.profiles.active}")
    public String springProfilesActive;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @PostConstruct
    public void init() {
        if ("alpha".equals(springProfilesActive)) {
            initAlpha();
        }
        if ("gamma".equals(springProfilesActive)) {
            initAlpha();
        }
    }

    private void initAlpha() {
        HhyTemplate hhyTemplate = new HhyTemplate();
        hhyTemplate.id = ApplicationConst.testTemplate1Id;
        hhyTemplate.name = ApplicationConst.testTemplate1Name;
        hhyTemplate.loginName = ApplicationConst.testTemplate1LoginName;
        hhyTemplate.password = ApplicationConst.testTemplate1Password;
        hhyTemplate.areaCode = ApplicationConst.testTemplate1AreaCode;
        hhyTemplate.phone = ApplicationConst.testTemplate1Phone;
        hhyTemplateService.put(hhyTemplate);
        for (int j = 0; j < 5; j++) {
            HhyBook book = new HhyBook();
            book.id = "id_" + hhyTemplate.id + "_" + j;
            book.name = "book_" + hhyTemplate.id + "_" + j;
            book.templateId = hhyTemplate.id;
            bookService.post(book);
        }

        //加载区域id
        List<HhyArea> areaList = loadAreaList();

        areaList.stream().forEach(hhyArea -> {
            hhyAreaService.put(hhyArea);
        });


        hhyProductCategoryService.put((createProductCategory("00000", "美食", "全部", HhyProductCategory.mallTypeUniteMall)));
        hhyProductCategoryService.put((createProductCategory("00001", "美食", "西餐", HhyProductCategory.mallTypeUniteMall)));
        hhyProductCategoryService.put((createProductCategory("00002", "美食", "中餐", HhyProductCategory.mallTypeUniteMall)));
        hhyProductCategoryService.put((createProductCategory("00003", "美食", "火锅", HhyProductCategory.mallTypeUniteMall)));
        hhyProductCategoryService.put((createProductCategory("00004", "美食", "日韩料理", HhyProductCategory.mallTypeUniteMall)));
        hhyProductCategoryService.put((createProductCategory("00005", "美食", "海鲜", HhyProductCategory.mallTypeUniteMall)));

        hhyProductCategoryService.put((createProductCategory("00100", "娱乐", "全部", HhyProductCategory.mallTypeUniteMall)));
        hhyProductCategoryService.put((createProductCategory("00101", "娱乐", "酒吧", HhyProductCategory.mallTypeUniteMall)));
        hhyProductCategoryService.put((createProductCategory("00102", "娱乐", "KTV", HhyProductCategory.mallTypeUniteMall)));

        hhyProductCategoryService.put((createProductCategory("00200", "酒店", "全部", HhyProductCategory.mallTypeUniteMall)));
        hhyProductCategoryService.put((createProductCategory("00201", "酒店", "客栈", HhyProductCategory.mallTypeUniteMall)));
        hhyProductCategoryService.put((createProductCategory("00202", "酒店", "公寓", HhyProductCategory.mallTypeUniteMall)));

        hhyProductCategoryService.put((createProductCategory("10001", "女装", "毛针织衫", HhyProductCategory.mallTypePointsMall)));
        hhyProductCategoryService.put((createProductCategory("10002", "女装", "打底裤", HhyProductCategory.mallTypePointsMall)));
        hhyProductCategoryService.put((createProductCategory("10003", "女装", "毛衣", HhyProductCategory.mallTypePointsMall)));
        hhyProductCategoryService.put((createProductCategory("10004", "女装", "毛呢外套", HhyProductCategory.mallTypePointsMall)));
        hhyProductCategoryService.put((createProductCategory("10005", "女装", "棉衣/棉服", HhyProductCategory.mallTypePointsMall)));
        hhyProductCategoryService.put((createProductCategory("10006", "女装", "马夹", HhyProductCategory.mallTypePointsMall)));

        hhyProductCategoryService.put((createProductCategory("10101", "男装", "T恤", HhyProductCategory.mallTypePointsMall)));
        hhyProductCategoryService.put((createProductCategory("10102", "男装", "休闲裤", HhyProductCategory.mallTypePointsMall)));
        hhyProductCategoryService.put((createProductCategory("10103", "男装", "衬衫", HhyProductCategory.mallTypePointsMall)));

        hhyProductCategoryService.put((createProductCategory("10201", "鞋靴", "女士低帮鞋", HhyProductCategory.mallTypePointsMall)));
        hhyProductCategoryService.put((createProductCategory("10202", "鞋靴", "女式凉鞋", HhyProductCategory.mallTypePointsMall)));
        hhyProductCategoryService.put((createProductCategory("10203", "鞋靴", "女士拖鞋", HhyProductCategory.mallTypePointsMall)));

        HhyManager testManager1 = new HhyManager();
        testManager1.id = ApplicationConst.testManager1Id;
        testManager1.name = ApplicationConst.testManager1Name;
        testManager1.loginName = ApplicationConst.testManager1LoginName;
        testManager1.password = ApplicationConst.testManager1Password;
        testManager1.phone = ApplicationConst.testManager1Phone;
        hhyManagerService.put(testManager1);

        HhyAgent testAgent1 = new HhyAgent();
        testAgent1.id = ApplicationConst.testAgent1Id;
        testAgent1.name = ApplicationConst.testAgent1Name;
        testAgent1.areaCode = ApplicationConst.testAgent1AreaCode;
        testAgent1.loginName = ApplicationConst.testAgent1LoginName;
        testAgent1.password = ApplicationConst.testAgent1Password;
        testAgent1.phone = ApplicationConst.testAgent1Phone;
        hhyAgentService.put(testAgent1);

        HhySeller testSeller1 = new HhySeller();
        testSeller1.id = ApplicationConst.testSeller1Id;
        testSeller1.name = ApplicationConst.testSeller1Name;
        testSeller1.areaCode = ApplicationConst.testSeller1AreaCode;
        testSeller1.loginName = ApplicationConst.testSeller1LoginName;
        testSeller1.password = ApplicationConst.testSeller1Password;
        testSeller1.phone = ApplicationConst.testSeller1Phone;
        hhySellerService.put(testSeller1);


        HhyProduct uniteMallProduct1 = new HhyProduct();
        uniteMallProduct1.id = ApplicationConst.testUniteMallProductId1;
        uniteMallProduct1.status = HhyProduct.statusUp;
        uniteMallProduct1.categoryId = "00001";//西餐
        uniteMallProduct1.title = "土豪的西餐";
        uniteMallProduct1.originalPrice = 58800;
        uniteMallProduct1.memberPrice = 38800;
        uniteMallProduct1.extraPoints = 200;
        uniteMallProduct1.sellerId = testSeller1.id;
        uniteMallProduct1.areaCode = testSeller1.areaCode;
        uniteMallProduct1.image = ApplicationConst.DemoImageURL1;
        uniteMallProduct1.mallType = HhyProduct.mallTypeUniteMall;
        uniteMallProduct1.reviewStatus = HhyProduct.reviewStatus1Success;

        HhyProduct uniteMallProduct2 = new HhyProduct();
        uniteMallProduct2.id = ApplicationConst.testUniteMallProductId2;
        uniteMallProduct2.status = HhyProduct.statusUp;
        uniteMallProduct2.categoryId = "00002";//中餐
        uniteMallProduct2.title = "好吃的中餐";
        uniteMallProduct2.originalPrice = 18800;
        uniteMallProduct2.memberPrice = 8800;
        uniteMallProduct2.extraPoints = 300;
        uniteMallProduct2.sellerId = testSeller1.id;
        uniteMallProduct2.areaCode = testSeller1.areaCode;
        uniteMallProduct2.image = ApplicationConst.DemoImageURL2;
        uniteMallProduct2.mallType = HhyProduct.mallTypeUniteMall;
        uniteMallProduct2.reviewStatus = HhyProduct.reviewStatus1Success;

        hhyProductService.put(uniteMallProduct1);
        hhyProductService.put(uniteMallProduct2);

        HhyProduct pointsMallProduct1 = new HhyProduct();
        pointsMallProduct1.id = ApplicationConst.testPointsMallProductId1;
        pointsMallProduct1.status = HhyProduct.statusUp;
        pointsMallProduct1.categoryId = "10001";//女装 毛衣
        pointsMallProduct1.title = "好毛衣";
        pointsMallProduct1.originalPrice = 158800;
        pointsMallProduct1.memberPrice = 138800;
        pointsMallProduct1.extraPoints = 2000;
        pointsMallProduct1.sellerId = testSeller1.id;
        pointsMallProduct1.image = ApplicationConst.DemoImageURL1;
        pointsMallProduct1.mallType = HhyProduct.mallTypePointsMall;
        pointsMallProduct1.reviewStatus = HhyProduct.reviewStatus1Success;

        HhyProduct pointsMallProduct2 = new HhyProduct();
        pointsMallProduct2.id = ApplicationConst.testPointsMallProductId2;
        pointsMallProduct2.status = HhyProduct.statusUp;
        pointsMallProduct2.categoryId = "10002";//女装 打底裤
        pointsMallProduct2.title = "好裤子";
        pointsMallProduct2.originalPrice = 118800;
        pointsMallProduct2.memberPrice = 108800;
        pointsMallProduct2.extraPoints = 3000;
        pointsMallProduct2.sellerId = testSeller1.id;
        pointsMallProduct2.image = ApplicationConst.DemoImageURL2;
        pointsMallProduct2.mallType = HhyProduct.mallTypePointsMall;
        pointsMallProduct2.reviewStatus = HhyProduct.reviewStatus1Success;

        hhyProductService.put(pointsMallProduct1);
        hhyProductService.put(pointsMallProduct2);

        HhyWallet memberHhyWallet = new HhyWallet();
        memberHhyWallet.phone = ApplicationConst.testMember1Phone;
        memberHhyWallet.enableWithdrawLimitPoints = 20000;
        memberHhyWallet.disableWithdrawPoints = 500;
        hhyWalletService.put(memberHhyWallet);

        HhyMember testMember1 = new HhyMember();
        testMember1.id = ApplicationConst.testMember1Id;
        testMember1.name = ApplicationConst.testMember1Name;
        testMember1.areaCode = ApplicationConst.testMember1AreaCode;
        testMember1.phone = ApplicationConst.testMember1Phone;
        testMember1.password = ApplicationConst.testMember1Password;
        testMember1.loginName = ApplicationConst.testMember1LoginName;
        testMember1.walletId = hhyWalletService.findIfByPhone(testMember1.phone).id;
        hhyMemberService.put(testMember1);

        HhyOrder hhyOrder1 = new HhyOrder();
        hhyOrder1.sellerId = testSeller1.id;
        hhyOrder1.memberId = testMember1.id;
        hhyOrder1.sellerName = testSeller1.name;
        hhyOrder1.status = HhyOrder.status0WaitingMemberPay;
        hhyOrderService.put(hhyOrder1);

        HhyOrder hhyOrder2 = new HhyOrder();
        hhyOrder2.sellerId = testSeller1.id;
        hhyOrder2.memberId = testMember1.id;
        hhyOrder2.sellerName = testSeller1.name;
        hhyOrder2.status = HhyOrder.status10WaitingSellerSendProduct;
        hhyOrderService.put(hhyOrder2);

        HhyOrder hhyOrder3 = new HhyOrder();
        hhyOrder3.sellerId = testSeller1.id;
        hhyOrder3.memberId = testMember1.id;
        hhyOrder3.sellerName = testSeller1.name;
        hhyOrder3.status = HhyOrder.status20WaitingMemberReceiveProduct;
        hhyOrderService.put(hhyOrder3);

        HhyOrder hhyOrder4 = new HhyOrder();
        hhyOrder4.sellerId = testSeller1.id;
        hhyOrder4.memberId = testMember1.id;
        hhyOrder4.sellerName = testSeller1.name;
        hhyOrder4.status = HhyOrder.status30FinishAndWaitingMemberComment;
        hhyOrderService.put(hhyOrder4);

        HhyAdvertisement hhyAdvertisement1 = new HhyAdvertisement();
        hhyAdvertisement1.id = "01";
        hhyAdvertisement1.content = ApplicationConst.DemoImageURL1;
        hhyAdvertisement1.areaCode = ApplicationConst.testAreaCodeDistrict;
        hhyAdvertisementService.put(hhyAdvertisement1);

        HhyAdvertisement hhyAdvertisement2 = new HhyAdvertisement();
        hhyAdvertisement2.id = "02";
        hhyAdvertisement2.content = ApplicationConst.DemoImageURL2;
        hhyAdvertisement2.areaCode = ApplicationConst.testAreaCodeDistrict;
        hhyAdvertisementService.put(hhyAdvertisement2);
    }

    private List<HhyArea> loadAreaList() {
        List<HhyArea> areaList = new Vector<>();
        try {
            InputStream inputStream = CommonUtil.readResource("area.json");
            String areaJson = IOUtils.toString(inputStream);

            List<Map> provinceList = JsonUtil.json2Bean(areaJson, List.class);
            for (Map mapProvince : provinceList) {
                String value = mapProvince.get("value").toString();
                String label = mapProvince.get("label").toString();
                String province = label;
                HhyArea area = new HhyArea();
                area.id = value;
                area.code = value;
                area.province = label;
                areaList.add(area);
                List<Map> cityList = (List<Map>) mapProvince.get("children");
                for (Map mapCity : cityList) {
                    String valueCity = mapCity.get("value").toString();
                    String labelCity = mapCity.get("label").toString();
                    String city = labelCity;
                    area = new HhyArea();
                    area.id = valueCity;
                    area.code = valueCity;
                    area.province = province;
                    area.city = labelCity;
                    areaList.add(area);
                    List<Map> districtList = (List<Map>) mapCity.get("children");
                    for (Map mapDistrict : districtList) {
                        String valueDistrict = mapDistrict.get("value").toString();
                        String labelDistrict = mapDistrict.get("label").toString();
                        area = new HhyArea();
                        area.id = valueDistrict;
                        area.code = valueDistrict;
                        area.province = province;
                        area.city = city;
                        area.district = labelDistrict;
                        areaList.add(area);
                    }
                }
            }
        } catch (Exception e) {
            log.error("", e);
        }
        return areaList;
    }

    private static HhyProductCategory createProductCategory(String id, String level1, String level2, int mallType) {
        HhyProductCategory hhyProductCategoryVO = new HhyProductCategory();
        hhyProductCategoryVO.id = id;
        hhyProductCategoryVO.level1Name = level1;
        hhyProductCategoryVO.level2Name = level2;
        hhyProductCategoryVO.mallType = mallType;
        return hhyProductCategoryVO;
    }
}
